package com.mairuide.one_click_search.service;

import com.mairuide.one_click_search.dao.OnekeyImportLogDao;
import com.mairuide.one_click_search.dto.DataTemplateDto;
import com.mairuide.one_click_search.pojo.OnekeyImportLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-05-10 15:48
 */
@Service
public class OnekeyImportLogService {
    @Autowired
    OnekeyImportLogDao onekeyImportLogDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    public void saveOnekeyImportLog(OnekeyImportLog onekeyImportLog) {
        onekeyImportLogDao.save(onekeyImportLog);
    }
    /**
     * @Author 刘中华
     * @Date 2019/4/26 11:06
     * @Param [dataTemplateDto]
     * @return void
     * @Description 得到分页数据
     **/
    public Page<OnekeyImportLog> getPageData(DataTemplateDto dataTemplateDto, int page, int size){
        Sort sort = new Sort(Sort.Direction.DESC,"vid");
        Specification<OnekeyImportLog> specification = createSpecification(dataTemplateDto);
        PageRequest pageRequest = new PageRequest(page-1<0?0:page-1,size,sort);
        return onekeyImportLogDao.findAll(specification, pageRequest);
    }
    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<OnekeyImportLog> createSpecification(DataTemplateDto dataTemplateDto) {
        return new Specification<OnekeyImportLog>() {
            @Override
            public Predicate toPredicate(Root<OnekeyImportLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(dataTemplateDto.getMobanname())) {
                    predicateList.add(cb.like(root.get("mobanname").as(String.class), "%"+dataTemplateDto.getMobanname()+"%"));
                }
                predicateList.add(cb.equal(root.get("isDelete").as(Integer.class), 0));
                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };

    }

    /**
     * @Author 刘中华
     * @Date 2019/4/23 16:59
     * @return  删除日志表，删除solr数据
     * @Description
     **/
    public void delOnekeyImportLog(Integer vid){
        OnekeyImportLog onekeyImportLog = onekeyImportLogDao.findOne(vid);
        if (onekeyImportLog!=null){
            String tablename = onekeyImportLog.getTablename();
            String sql= "update TB_ONEKEY_IMPORT_LOG set IS_DELETE = 1 where TABLENAME= '"+tablename+"'";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(new Date());
            String sql2= "update TB_DATA_SOURCE set IS_DELETE = 1 , CREATE_TIME=to_date('"+format+"','yyyy-mm-dd hh24:mi:ss') where TABLENAME= '"+tablename+"'";
            jdbcTemplate.execute(sql);
            jdbcTemplate.execute(sql2);
        }
    }
}
